package org.ht.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.azj.dbOperatorComponent.CompositeTable;
import org.azj.dbOperatorComponent.ICommand;
import org.azj.dbOperatorComponent.IDbQueryOperation;
import org.azj.dbOperatorComponent.JoinType;
import org.azj.dbOperatorComponent.OperableTable;
import org.azj.dbOperatorComponent.OrderByPair;
import org.azj.dbOperatorComponent.OrderByType;
import org.azj.dbOperatorComponent.SelectFilter;
import org.ht.database.DataBase;
import org.ht.entity.QueryEntity;
import org.ht.exception.LimitException;
import org.ht.global.DbTableNames;
import org.ht.viewer.QueryDialog;

public class ControlQueryDialog {
	public static final int RESULT_OK = 0; // 查询成功
	public static final int RESULT_CHANGE = 1; // 数据类型转换错误
	public static final int RESULT_DB = 2; // 数据库或后台报错
	public static final int RESULT_LIMIT = 3; // 控件左右大小不对,超范围
	public static final int RESULT_NULL = 4; // 控件全为空

	// public static final String

	private QueryDialog queryDialog;

	public ControlQueryDialog(QueryDialog queryDialog) {
		this.queryDialog = queryDialog;
	}

	public String getDefault(String key) {
		String result = "";
		switch (key) {
		case "AgeInHospital":
			result = "1";
			break;
		case "DisBreakOutDt":
			result = "1";
			break;
		case "BodyTemprature":
			result = "1";
			break;
		case "PreOpWBC":
			result = "1";
			break;
		case "PreOpN":
			result = "1";
			break;
		case "PreOpCRP":
			result = "1";
			break;
		case "PreOpBloodSugar":
			result = "1";
			break;
		case "PostOpBodyTemp":
			result = "1";
			break;
		default:
			result = "";
			break;
		}
		return result;
	}

	private Map<String, String> qemap;

	public int check(QueryEntity qe) {
		qemap = new HashMap<String, String>();
		try {
			String s = null;

			s = qe.getAgeInHospital1();
			if (!s.isEmpty()) {
				int d1 = Integer.valueOf(qe.getAgeInHospital1());
				int d2 = Integer.valueOf(qe.getAgeInHospital2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("AgeInHospital", d1 + "@" + d2);
			}

			s = qe.getDisBreakOutDt1();
			if (!s.isEmpty()) {
				int d1 = Integer.valueOf(qe.getDisBreakOutDt1());
				int d2 = Integer.valueOf(qe.getDisBreakOutDt2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("DisBreakOutDt", d1 + "@" + d2);
			}

			s = qe.getBodyTemprature1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getBodyTemprature1());
				double d2 = Double.valueOf(qe.getBodyTemprature2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("BodyTemprature",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}
			s = qe.getPreOpWBC1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getPreOpWBC1());
				double d2 = Double.valueOf(qe.getPreOpWBC2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("PreOpWBC",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}
			s = qe.getPreOpN1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getPreOpN1());
				double d2 = Double.valueOf(qe.getPreOpN2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("PreOpN",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}
			s = qe.getPreOpCRP1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getPreOpCRP1());
				double d2 = Double.valueOf(qe.getPreOpCRP2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("PreOpCRP",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}
			s = qe.getPreOpBloodSugar1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getPreOpBloodSugar1());
				double d2 = Double.valueOf(qe.getPreOpBloodSugar2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("PreOpBloodSugar",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}
			s = qe.getPostOpBodyTemp1();
			if (!s.isEmpty()) {
				double d1 = Double.valueOf(qe.getPostOpBodyTemp1());
				double d2 = Double.valueOf(qe.getPostOpBodyTemp2());
				if (d1 >= d2) {
					throw new LimitException();
				}
				qemap.put("PostOpBodyTemp",
						String.valueOf(d1) + "@" + String.valueOf(d2));
			}

			s = qe.getSex();
			if (!s.isEmpty()) {
				String d1 = qe.getSex();
				if (!d1.isEmpty()) {
					qemap.put("Sex", "Sex" + "@" + d1);
				}
			}

			String d1 = qe.getDisScale();
			if (!d1.isEmpty()) {
				switch (d1) {
				case "1-2个象限":
					qemap.put("DisScale", "DisScale12" + "@" + "true");
					break;
				case "2-3个象限":
					qemap.put("DisScale", "DisScale23" + "@" + "true");
					break;
				case "3-4个象限":
					qemap.put("DisScale", "DisScale34" + "@" + "true");
					break;
				}
			}

			d1 = qe.getIncision();
			if (!d1.isEmpty()) {
				switch (d1) {
				case "单纯小切口":
					qemap.put("Incision", "SimpleIncision" + "@" + "true");
					break;
				case "多点小切口":
					qemap.put("Incision", "MultiIncision" + "@" + "true");
					break;
				case "全范围切开":
					qemap.put("Incision", "FullScaleIncision" + "@" + "true");
					break;
				}
			}

			d1 = qe.getHasInnercut();
			if (!d1.isEmpty()) {
				switch (d1) {
				case "有":
					qemap.put("HasInnercut", "HasInnercut" + "@" + "true");
					break;
				case "无":
					qemap.put("HasInnercut", "HasInnercut" + "@" + "false");
					break;
				}
			}
			
			d1 = qe.getHasPusCultivation();
			if (!d1.isEmpty()) {
				switch (d1) {
				case "有":
					qemap.put("HasPusCultivation", "HasPusCultivation" + "@" + "true");
					break;
				case "无":
					qemap.put("HasPusCultivation", "HasPusCultivation" + "@" + "false");
					break;
				}
			}

			boolean b = qe.isHealOpIgnore();
			if (!b){
				if (qe.isHealOpCathetering()){
					qemap.put("HealOpCathetering", "HealOpCathetering" + "@" + "true");
				}else{
					qemap.put("HealOpCathetering", "HealOpCathetering" + "@" + "false");
				}
				if (qe.isHealOpDraggingWires()){
					qemap.put("HealOpDraggingWires", "HealOpDraggingWires" + "@" + "true");
				}else{
					qemap.put("HealOpDraggingWires", "HealOpDraggingWires" + "@" + "false");
				}
				if (qe.isHealOpSeton()){
					qemap.put("HealOpSeton", "HealOpSeton" + "@" + "true");
				}else{
					qemap.put("HealOpSeton", "HealOpSeton" + "@" + "false");
				}
			}
			
		} catch (NumberFormatException e) {
			return RESULT_CHANGE;
		} catch (LimitException e) {
			return RESULT_LIMIT;
		}
		if (qemap.isEmpty()) {
			return RESULT_NULL;
		}
		return RESULT_OK;
	}

	public Map<String, String> getQemap() {
		return qemap;
	}

	public void setQemap(Map<String, String> qemap) {
		this.qemap = qemap;
	}

	private static CompositeTable showTable = null;

	public List<String> query(String where) throws SQLException {
		List<String> list = new ArrayList<String>();
		ICommand command = null;

		if (showTable == null) {
			generateShowTable();
		}
		IDbQueryOperation queryDb = (IDbQueryOperation) showTable;
		SelectFilter selectFilter = new SelectFilter();
		selectFilter
				.setColumnList(DbTableNames.FullTblBasicInfo + ".PATIENTID");
		selectFilter.setWhereFilter(where);
		
		OrderByPair orderByPair = 
				new OrderByPair(
						DbTableNames.FullTblBasicInfo + ".PatientId", 
						OrderByType.ASC);
		List<OrderByPair> obp = new ArrayList<OrderByPair>();
		obp.add(orderByPair);
		selectFilter.setOrderByList(obp);

		
		command = queryDb.getSelectCommand(selectFilter);

		List<HashMap<String, Object>> rs = command.doCommand(DataBase
				.getDataBase());
		for (HashMap<String, Object> hashMap : rs) {
			// int i = (int)hashMap.get(DbTableNames.FullTblBasicInfo +
			// ".PATIENTID");
			list.add(String.valueOf(hashMap.get(DbTableNames.FullTblBasicInfo
					+ ".PATIENTID")));
		}

		return list;

	}

	private void generateShowTable() throws SQLException {
		IDbQueryOperation tblBasicInfo = new OperableTable(
				DataBase.getDataBase(), DbTableNames.SchemaName,
				DbTableNames.TblBasicInfo);
		IDbQueryOperation tblPreOp = new OperableTable(DataBase.getDataBase(),
				DbTableNames.SchemaName, DbTableNames.TblBeforeOp);
		IDbQueryOperation tblMidOp = new OperableTable(DataBase.getDataBase(),
				DbTableNames.SchemaName, DbTableNames.TblMidOp);
		IDbQueryOperation tblPostOp = new OperableTable(DataBase.getDataBase(),
				DbTableNames.SchemaName, DbTableNames.TblPostOp);
		IDbQueryOperation tblImage = new OperableTable(DataBase.getDataBase(),
				DbTableNames.SchemaName, DbTableNames.TblImage);

		showTable = new CompositeTable("LH_TEMP", "CompositeTable");
		showTable.addComponentTable(tblBasicInfo, null, null);
		showTable.addComponentTable(
				tblPreOp,
				JoinType.LEFT_OUTER_JOIN,
				tblBasicInfo.getTableFullName() + ".PatientId = "
						+ tblPreOp.getTableFullName() + ".PatientId");
		showTable.addComponentTable(
				tblMidOp,
				JoinType.LEFT_OUTER_JOIN,
				tblBasicInfo.getTableFullName() + ".PatientId = "
						+ tblMidOp.getTableFullName() + ".PatientId");
		showTable.addComponentTable(
				tblPostOp,
				JoinType.LEFT_OUTER_JOIN,
				tblBasicInfo.getTableFullName() + ".PatientId = "
						+ tblPostOp.getTableFullName() + ".PatientId");
		showTable.addComponentTable(
				tblImage,
				JoinType.LEFT_OUTER_JOIN,
				tblBasicInfo.getTableFullName() + ".PatientId = "
						+ tblImage.getTableFullName() + ".PatientId");
	}
}
